package com.yss.reportworld.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;

import com.yss.common.core.utils.StringUtils;
import com.yss.common.core.utils.easyexecl.EasyExeclUtil;
import com.yss.common.core.utils.uuid.IdUtils;
import com.yss.common.security.utils.SecurityUtils;
import com.yss.reportworld.enums.ReportWorldStandardCheckTableCodeEnum;
import com.yss.reportworld.service.ICustomerService;
import com.yss.reportworld.service.IModularService;
import com.yss.reportworld.service.IReportService;
import com.yss.system.api.domain.SysDictData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yss.common.logger.annotation.Log;
import com.yss.common.logger.enums.BusinessType;
import com.yss.common.security.annotation.RequiresPermissions;
import com.yss.reportworld.domain.TableStandardCheck;
import com.yss.reportworld.service.ITableStandardCheckService;
import com.yss.common.core.web.controller.BaseController;
import com.yss.common.core.web.domain.AjaxResult;
import com.yss.common.core.web.page.TableDataInfo;

/**
 * 表校验规则Controller
 *
 * @author yss
 * @date 2023-04-07
 */
@RestController
@RequestMapping("/tableStandardcheck" )
public class TableStandardCheckController extends BaseController {
    @Autowired
    private ITableStandardCheckService tableStandardCheckService;
    @Autowired
    private IModularService modularService;
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IReportService reportService;

    /**
     * 查询表校验规则列表
     */
    @RequiresPermissions("reportworld:tableStandardcheck:list" )
    @GetMapping("/list" )
    public TableDataInfo list(TableStandardCheck tableStandardCheck) {
        startPage();
        List<TableStandardCheck> list = tableStandardCheckService.selectTableStandardCheckList(tableStandardCheck);
        return getDataTable(list);
    }

    /**
     * 初始化 加载
     */
    @RequiresPermissions("reportworld:tableStandardcheck:list" )
    @Log(title = "表明细信息" , businessType = BusinessType.OTHER)
    @PutMapping("/init" )
    public AjaxResult init() {
        // 标准表校验类型
        List<String> listCheck = ReportWorldStandardCheckTableCodeEnum.getReportWorldStandardCheckTableEnum();
        List<SysDictData> listCheckTableCode = new ArrayList<>();
        if (!StringUtils.isEmpty(listCheck)) {
            for (String code : listCheck) {
                SysDictData dicvalue = new SysDictData();
                dicvalue.setDictValue(code);
                dicvalue.setDictLabel(ReportWorldStandardCheckTableCodeEnum.getEnumDesc(code));
                listCheckTableCode.add(dicvalue);
            }
        }
        AjaxResult ajax = AjaxResult.success();
        //加载所属模块，客户，报表
        ajax.put("modularTree" , modularService.selectModularTreeList());
        ajax.put("customerTree" , customerService.selectCustomerTreeList());
        ajax.put("reportTree" , reportService.selectReportTreeList());
        ajax.put("listCheckTableCode" , listCheckTableCode);
        return ajax;
    }


    /**
     * 导出表校验规则列表
     */
    @RequiresPermissions("reportworld:tableStandardcheck:export" )
    @Log(title = "表校验规则" , businessType = BusinessType.EXPORT)
    @PostMapping("/export" )
    public void export(HttpServletResponse response, TableStandardCheck tableStandardCheck) {
        List<TableStandardCheck> list = tableStandardCheckService.selectTableStandardCheckList(tableStandardCheck);
        EasyExeclUtil.export(response, list, TableStandardCheck.class );
    }

    /**
     * 获取表校验规则详细信息
     */
    @RequiresPermissions("reportworld:tableStandardcheck:query" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) String id) {
        return success(tableStandardCheckService.getById(id));
    }

    /**
     * 新增表校验规则
     */
    @RequiresPermissions("reportworld:tableStandardcheck:add" )
    @Log(title = "表校验规则" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated TableStandardCheck tableStandardCheck) {
        if (StringUtils.isEmpty(tableStandardCheck.getTableNamePrefix()) &&
                StringUtils.isEmpty(tableStandardCheck.getTableNameSuffix()) &&
                StringUtils.isEmpty(tableStandardCheck.getTableNameContain())) {
            return error("表名前缀，表名后缀，表名包含，至少有一个不能为空" );
        }
        if (!tableStandardCheckService.checkTableStandardUnique(tableStandardCheck)) {
            return error("校验规则已存在" );
        }
        tableStandardCheck.setId(IdUtils.fastSimpleUUID());
        tableStandardCheck.setCreateBy(SecurityUtils.getUsername());
        tableStandardCheck.setCreateTime(new Date());
        return toAjax(tableStandardCheckService.save(tableStandardCheck));
    }

    /**
     * 修改表校验规则
     */
    @RequiresPermissions("reportworld:tableStandardcheck:edit" )
    @Log(title = "表校验规则" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated TableStandardCheck tableStandardCheck) {
        if (StringUtils.isEmpty(tableStandardCheck.getTableNamePrefix()) &&
                StringUtils.isEmpty(tableStandardCheck.getTableNameSuffix()) &&
                StringUtils.isEmpty(tableStandardCheck.getTableNameContain())) {
            return error("表名前缀，表名后缀，表名包含，至少有一个不能为空" );
        }
        if (!tableStandardCheckService.checkTableStandardUnique(tableStandardCheck)) {
            return error("校验规则已存在" );
        }
        tableStandardCheck.setUpdateTime(new Date());
        tableStandardCheck.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(tableStandardCheckService.updateById(tableStandardCheck));
    }

    /**
     * 删除表校验规则
     */
    @RequiresPermissions("reportworld:tableStandardcheck:remove" )
    @Log(title = "表校验规则" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable List<String> ids) {
        return toAjax(tableStandardCheckService.removeByIds(ids));
    }
}
